const handoverService = require('../service/handoverService')
const code = require('../common/code')
const KoaRouter = require('koa-router')
const auth = require('../middleware/auth')
// const Excel = require('exceljs/modern.nodejs')
// const dayjs = require('dayjs')

const router = new KoaRouter()

router.get('/handoverList', auth(['EDP']), async ctx => {
    ctx.body = code.getCode(code.SUCCESS, null, await handoverService.getHandoverList(ctx.session.user.store_id, ctx.query))
})

router.get('/handover/:id', auth(['EDP']), async ctx => {
    ctx.body = code.getCode(code.SUCCESS, null, await handoverService.getHandover(ctx.params.id, ctx.session.user.store_id))
})

router.post('/handover', auth(['EDP']), async ctx => {
    const handover = ctx.request.body
    if (await handoverService.checkHandover(null, handover, ctx.session.user.store_id)) {
        ctx.body = code.getCode(code.VALIDATE_ERROR, '该班次已存在')
    } else {
        if (handover.shift === 1) {
            handover.nr_begin = null
            handover.nr_end = null
        }
        handover.user_id = ctx.session.user.id
        await handoverService.insertHandover(handover)
        ctx.body = code.getCode(code.SUCCESS)
    }
})

router.put('/handover/:id', auth(['EDP']), async ctx => {
    const handover = ctx.request.body
    if (await handoverService.checkHandover(ctx.params.id, handover, ctx.session.user.store_id)) {
        ctx.body = code.getCode(code.VALIDATE_ERROR, '该班次已存在')
    } else {
        if (handover.shift === 1) {
            handover.nr_begin = null
            handover.nr_end = null
        }
        await handoverService.updateHandover(ctx.params.id, handover)
        ctx.body = code.getCode(code.SUCCESS)
    }
})

// router.get('/handoverListWeek', auth(['USER']), async ctx => {
//     const begin = ctx.query.week
//     const end = dayjs(begin).add(7, 'day').format('YYYY-MM-DD')
//     ctx.body = code.getCode(code.SUCCESS, null, await handoverService.getHandoverListWeek(ctx.session.user.store_id, begin, end))
// })

// router.get('/handoverExport', async ctx => {
//     const begin = ctx.query.week
//     const end = await formatDate(begin)
//     const handoverList = await handoverService.getHandoverListWeek(ctx.session.user.store_id, begin, end)
//     if (handoverList.length) {
//         const workbook = new Excel.Workbook()
//         await workbook.xlsx.readFile('./src/common/handover.xlsx')
//         // await workbook.xlsx.writeFile('./src/common/export.xlsx')
//         for (let i = 0; i < handoverList.length; i++) {
//             let handover = handoverList[i]
//             let worksheet = workbook.getWorksheet(1 + Math.floor(i / 2))
//             worksheet.getCell(`A${i%2 === 0 ? 2 : 18}`).value = `${dayjs(handover.date).format('YYYY年MM月DD日   星期d')}                                              ${handover.shift === 1 ? '早班' : '晚班'}：${handover.User.name}`
//             worksheet.getCell(`A${i%2 === 0 ? 3 : 19}`).value = `                                            机房温度：${handover.temperature}℃              机房湿度：${handover.humidity}%`
//         }
//         ctx.body = code.getCode(code.SUCCESS, null, await workbook.xlsx.writeBuffer())
//         // ctx.body = await workbook.xlsx.writeBuffer()
//     } else {
//         ctx.body = code.getCode(code.VALIDATE_ERROR, '没有交接')
//     }
// })

module.exports = router
